<script setup>
import topNav from "../../components/topNav.vue";
import { introApi } from "@/services/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import { appInfo, loginSwitch } from "@/services/user";
// import topNav from "../../components/topNav.vue";
import { operation } from "@/utils/num.js";
import {
  scTransferSign,
  scTransferRemind,
  appScTransferReturnQuery,
} from "@/services/transfer";
import {
  getAdjustDamageDetail,
  getApprovePass,
  getApproveRefuse,
} from "@/services/cashier";

const showSelect = ref("");

if (uni.getStorageSync("selectShopType")) {
  showSelect.value = uni.getStorageSync("selectShopType");
}

const id = ref("");
const type = ref(1);
onLoad((e) => {
  id.value = e.id;
  // type.value = e.type ? e.type : 1;

  // getAppScTransferReturnQuery();
});
onShow(() => {
  getgetAdjustDamageDetail();
  // getAppScTransferReturnQuery();
});

const optionDet = ref({});
const returnList = ref([]);

const getgetAdjustDamageDetail = async () => {
  try {
    let res = await getAdjustDamageDetail({ id: id.value });
    optionDet.value = res.data;
  } catch (e) {}
};

const getAppScTransferReturnQuery = async () => {
  try {
    let res = await appScTransferReturnQuery({ id: id.value });
    returnList.value = res.data;
  } catch (e) {}
};

const secsShow = ref(false);
const toReturn = () => {
  secsShow.value = true;
};
const secsConfirm = async () => {
  try {
    let res = await getApprovePass({ id: id.value });
    if (res.code == 200) {
      uni.showToast({
        title: "审核成功",
        icon: "none",
      });

      getgetAdjustDamageDetail();
      secsShow.value = false;
    }
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
  }
};

//签收

const passwordShow = ref(false);
const isshowpassword = ref(false);
// const selectItem = ref({});
const password = ref("");

const showQs = () => {
  passwordShow.value = true;
  isshowpassword.value = false;
  password.value = "";
  // selectItem.value = item;
};
const isUp = ref(false);
const qrUpSign = async () => {
  if (!password.value) {
    uni.showToast({
      title: "请输入登录密码",
      icon: "none",
    });
    return;
  }

  if (isUp.value) {
    return;
  }
  isUp.value = true;
  try {
    let res = await scTransferSign({
      id: id.value,
      password: password.value,
    });
    if (res.code == 200) {
      uni.showToast({
        title: "签收成功",
        icon: "none",
      });
      optionDet.value.status = 12;
      isUp.value = false;
      passwordShow.value = false;
      // getAppProductQuery(1);
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none",
      });
      isUp.value = false;
    }

    console.log(res);
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
    isUp.value = false;
  }
};

const refuseReason = ref("");
const toSign = async () => {
  passwordShow.value = true;
};

const qrUpRefuse = async () => {
  try {
    let res = await getApproveRefuse({
      id: id.value,
      refuseReason: refuseReason.value,
    });
    if (res.code == 200) {
      uni.showToast({
        title: "拒绝成功",
        icon: "none",
      });

      getgetAdjustDamageDetail();
      passwordShow.value = false;
    }
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
  }
};

const previewImage = (item, index) => {
  console.log(item, index);
  uni.previewImage({
    current: index,
    urls: item,
  });
};

//提醒收货
</script>

<template>
  <view class="bg">
    <topNav
      :title="'调货记录'"
      @save="saveFun"
      :border="true"
      rType="save"
    ></topNav>

    <view class="top">
      <view class="top_left">
        <view class="top_title">
          <text> {{ optionDet.statusText }} </text>
        </view>
        <view class="top_dn" v-if="optionDet.description"
          >{{ optionDet.status == 0 ? "备注:" + optionDet.description : "" }}
        </view>
        <view
          class="top_dn"
          v-if="optionDet.refuseReason && optionDet.status == 6"
          >{{
            optionDet.status == 6 ? "拒绝原因:" + optionDet.refuseReason : ""
          }}
        </view>
      </view>
      <view class="top_right">
        <image
          v-if="optionDet.status == 0"
          src="@/static/cashier/statepage.png"
          mode="scaleToFill"
        />
        <image
          v-if="optionDet.status == 6"
          src="@/static/cashier/npage.png"
          mode="scaleToFill"
        />
        <image
          v-if="optionDet.status == 3"
          src="@/static/cashier/yespage.png"
          mode="scaleToFill"
        />
      </view>
    </view>

    <view class="info">
      <view class="info_title">{{ optionDet.scName }}</view>
      <view class="info_dns">单据编号：{{ optionDet.id }}</view>
      <view class="info_dh">
        <text
          ><text class="color">申请人:</text> {{ optionDet.createBy }}
        </text>
        <text
          ><text class="color">报损时间:</text>{{ optionDet.createTime }}</text
        >
      </view>

      <scroll-view
        class="scroll-view_H"
        scroll-x="true"
        @scroll="scroll"
        scroll-left="120"
      >
        <view class="info_list">
          <view class="info_item title">
            <view class="info_item_title">商品名称</view>
            <view class="info_item_num">供货价</view>
            <view class="info_item_uit">数量</view>
            <view class="info_item_pr1">单位</view>
            <view class="info_item_pr2">金额</view>
            <view class="info_item_pr3">报损理由</view>

            <view class="info_item_price">报损凭证</view>
          </view>
          <view
            class="info_item texts"
            v-for="(item, index) in optionDet.details"
            :key="index"
          >
            <view class="info_item_title"
              ><text>{{ item.productName }}</text>
            </view>
            <view class="info_item_num">{{ item.taxPrice }}</view>
            <view class="info_item_uit">{{ item.stockNum }}</view>
            <view class="info_item_pr1">{{ item.unit }}</view>
            <view class="info_item_pr2">{{ item.damageTotalPrice }}</view>
            <view class="info_item_pr3">{{ item.damageReasonName }}</view>
            <view class="info_item_price">
              <image
                @click="previewImage(item.damageCertificate, index)"
                v-for="(item2, index) in item.damageCertificate"
                :key="index"
                :src="item2"
                mode="scaleToFill"
              />
            </view>
          </view>
        </view>
      </scroll-view>
      <view class="info_item-fo">
        <view class="info_item_left">共 {{ optionDet.totalNum }} 件</view>
        <view class="info_item_center">总价:</view>
        <view class="info_item_right">¥{{ optionDet.totalAmount }}</view>
      </view>
    </view>

    <view class="det" v-if="optionDet.status == 6 || optionDet.status == 3">
      <view class="det_title">报损详情</view>

      <view class="det_info det_info2">
        <view class="det_info_item">审批人</view>
        <text>{{ optionDet.updateBy || "--" }}</text>
      </view>
      <view class="det_info">
        <view class="det_info_item">审批状态</view>
        <text>{{ optionDet.statusText || "--" }}</text>
      </view>
      <view class="det_info">
        <view class="det_info_item">审批时间</view>
        <text>{{ optionDet.updateTime || "--" }}</text>
      </view>
      <view class="det_info" v-if="optionDet.status == 6">
        <view class="det_info_item">拒绝原因</view>
        <text>{{ optionDet.refuseReason || "--" }}</text>
      </view>
    </view>

    <view class="det" v-if="optionDet.status == 0">
      <view class="det_title">报损详情</view>

      <view class="det_info">
        <view class="det_info_item">审批状态</view>
        <text>{{ optionDet.statusText || "--" }}</text>
      </view>
    </view>
  </view>

  <!-- v-if="type == 1" -->

  <view
    class="bt_form"
    v-permission="[
      'manager:stock:adjust:damage:approve:pass',
      'manager:stock:adjust:damage:approve:refuse',
    ]"
    v-if="showSelect == 'admin' && optionDet.status == 0"
  >
    <view class="bt_form_k">
      <up-button
        v-permission="['manager:stock:adjust:damage:approve:refuse']"
        v-if="optionDet.status == 0"
        class="bt_close"
        type="default"
        :plain="true"
        :hairline="true"
        text="拒绝报损"
        @click="toSign"
      ></up-button>
      <!-- v-permission="['manager:scTransfer:return:create']" -->
      <up-button
        v-if="optionDet.status == 0"
        v-permission="['manager:stock:adjust:damage:approve:pass']"
        class="bt_up"
        type="default"
        :plain="true"
        :hairline="true"
        text="通过报损"
        @click="toReturn"
      ></up-button>
    </view>
  </view>
  <!-- v-permission="['manager:scTransfer:sign']" -->

  <!-- <up-popup
    :show="passwordShow"
    round="15"
    mode="center"
    @close="passwordShow = false"
  >
    <view class="pop-content">
      <view class="pop-content-info">
        <view class="title"> 请输入登录密码校验 </view>
        <view class="content-to">
          <up-input
            class="input"
            placeholder="请输入登录密码"
            suffixIcon="map-fill"
            v-model="password"
            suffixIconStyle="color: #909399"
            :type="!isshowpassword ? 'password' : 'text'"
          >
            
            <template #suffix>
              <up-icon
                @click="isshowpassword = !isshowpassword"
                :name="!isshowpassword ? 'eye-fill' : 'eye-off'"
                color="#86909C"
                size="36rpx"
              ></up-icon>
            </template>
          </up-input>
        </view>
      </view>

      <view class="btns">
        <view class="btns-bt" @click="passwordShow = false">取消</view>

        <view class="btns-qd" @click="qrUpSign">确定</view></view
      >
    </view>
  </up-popup> -->

  <up-popup
    :show="passwordShow"
    round="15"
    mode="bottom"
    @close="passwordShow = false"
    :closeable="true"
  >
    <view class="pop-refuse">
      <view class="title"> 拒绝报损原因 </view>
      <view class="content">
        <up-textarea
          class="textarea"
          v-model="refuseReason"
          placeholder="选填"
          count
        ></up-textarea>
      </view>

      <view class="btns">
        <up-button
          @click="passwordShow = false"
          class="btns-bt"
          text="取消"
        ></up-button>
        <up-button @click="qrUpRefuse" class="btns-qd" text="确定"></up-button>
      </view>
    </view>
  </up-popup>

  <up-modal
    :show="secsShow"
    :title="'提示'"
    :content="'是否确认通过审核?'"
    :showCancelButton="true"
    @confirm="secsConfirm"
    @cancel="secsShow = false"
  ></up-modal>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
